/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.gui.table;

import com.camachosanchez.bean.Talleres;
import java.util.LinkedList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author Zully
 */
public class ModeloTablaTalleres implements TableModel {

    private LinkedList datos = new LinkedList();
    private LinkedList listeners = new LinkedList();

    @Override
    public int getRowCount() {
        return datos.size();
    }

    @Override
    public int getColumnCount() {
        return 7;
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "";
            case 1:
                return "CÓDIGO";
            case 2:
                return "NOMBRE";
            case 3:
                return "DIRECCIÓN";
            case 4:
                return "TELÉFONO";
            case 5:
                return "CORREO";
            case 6:
                return "ENCARGADO";
            default:
                return "";
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Talleres aux;
        aux = (Talleres) (datos.get(rowIndex));
        String texto = "";
        switch (columnIndex) {
            case 1:
                texto = "" + aux.getId();
                break;
            case 2:
                texto = aux.getNombre();
                break;
            case 3:
                texto = aux.getDireccion();
                break;
            case 4:
                texto = aux.getTelefono();
                break;
            case 5:
                texto = aux.getCorreo();
                break;
            case 6:
                texto = aux.getNombreEncargado() + " " + aux.getApellidoEncargado();
                break;
            default:
                break;
        }
        return texto;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        Talleres aux;
        aux = (Talleres) (datos.get(rowIndex));
        switch (columnIndex) {
            case 1:
                aux.setId(Integer.parseInt(aValue.toString()));
                break;
            case 2:
                aux.setNombre("" + aValue);
                break;
            case 3:
                aux.setDireccion("" + aValue);
                break;
            case 4:
                aux.setTelefono("" + aValue);
                break;
            case 5:
                aux.setCorreo("" + aValue);
                break;
        }
        TableModelEvent evento = new TableModelEvent(this, rowIndex, rowIndex, columnIndex);
        avisaSuscriptores(evento);
    }

    private void avisaSuscriptores(TableModelEvent evento) {
        int i;
        for (i = 0; i < listeners.size(); i++) {
            ((TableModelListener) listeners.get(i)).tableChanged(evento);
        }
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        listeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        listeners.remove(l);
    }

    public void agregarTaller(Talleres evt) {
        datos.add(evt);
        TableModelEvent evento;
        evento = new TableModelEvent(this, this.getRowCount() - 1, this.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        avisaSuscriptores(evento);
    }

    public void actualizarTaller(Talleres evt, int row) {
        datos.set(row, evt);
        TableModelEvent evento;
        evento = new TableModelEvent(this, this.getRowCount() - 1, this.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        avisaSuscriptores(evento);
    }

//    public int obtenerId(int rowIndex) {
//        Talleres aux;
//        aux = (Talleres) (datos.get(rowIndex));
//        return aux.getId();
//    }
//    public Talleres obtenerTaller(int rowIndex) {
//        Talleres aux;
//        aux = (Talleres) (datos.get(rowIndex));
//        return aux;
//    }
    public void borrarAccion(int fila) {
        datos.remove(fila);
        TableModelEvent evento = new TableModelEvent(this, fila, fila, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
        avisaSuscriptores(evento);
    }

    public void limpiar() {
        datos.clear();
        TableModelEvent evento;
        evento = new TableModelEvent(this, this.getRowCount() - 1, this.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE);
        avisaSuscriptores(evento);
    }
}
